package com.GamersCom.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.GamersCom.db.User;
import com.GamersCom.model.MgrFactory;
import com.GamersCom.model.UserMgr;
import com.GamersCom.util.GlobalCONST;

/**
 * Servlet implementation class AdminUsers
 */
public class AdminUsers extends HttpServlet
{
	private static final long serialVersionUID = 1L;
	
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException
	{
		process(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException
	{
		process(request, response);

	}

	protected void process(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException
	{
		HttpSession session = request.getSession();
		String username = (String) session.getAttribute("username");
		Integer access = (Integer) session.getAttribute("accesslevel");

		String nextView;
		
		if (username == null) // if user is not logged in
		{
			// redirect to the login page, if session expires.
//			String error = "Please Login first! <a href=\"../login\"> Login </a>";
//			request.setAttribute("error", error);
			nextView = "/login.jsp";
		}
		else
		{
			// authenticate the user by accessLevel
			if ( GlobalCONST.ADMINISTRATOR != access )
			{
				String error = "You are not allowed to view this page!";
				request.setAttribute("error", error);
				nextView = "result.jsp";
			}
			else
			{
				
				MgrFactory factory = (MgrFactory) this.getServletContext()
						.getAttribute("mgrFactory");
				UserMgr userMgr = factory.getUserMgr();
				
				
				String post = request.getParameter("submit");
				// handles the post method
				if (post != null)
				{
					int userId = Integer.parseInt(request.getParameter("id"));
					
					if ( post.equals("Approve"))
					{
						userMgr.approve(userId);
						request.setAttribute("msg", "The user has been approved!");
					}
					else if (post.equals("Ban"))
					{
						// update user status											
//						userMgr.approve(userId, GlobalCONST.BANNED);
						userMgr.ban(userId);
						request.setAttribute("msg", "The user has been banned!");
					}
					else if (post.equals("Delete"))
					{
						// delete user record						
						userMgr.reject(userId);
						request.setAttribute("msg", "The user has been deleted!");
					}

				}
				// display the user list

				List<User> userList = userMgr.getAllUsers();
				request.setAttribute("userList", userList);

				nextView = "/adminUsers.jsp";
			}
		}
		request.getRequestDispatcher(nextView).forward(request, response);

	}

}
